登录 白背景

372. 超级次方

https://leetcode-cn.com/problems/super-pow/

  • 提交时间:2021-12-05 14:52:23
  • 执行用时:8 ms, 在所有 Go 提交中击败了92.06%的用户
  • 内存消耗:3.6 MB, 在所有 Go 提交中击败了88.89%的用户
  • 通过测试用例:55 / 55
const mod = 1337

func superPow(a int, b []int) (ans int) {
    a = a % 1337
    ans = 1
    //低位到高位
    for i := len(b) - 1; i > -1; i-- {
        //求解
        ans = ans * quickPow(a, b[i]) % mod
        a = quickPow(a, 10) % mod
    }
    return
}

func quickPow(x int, n int) (ans int) {
    ans = 1
    x_contribute := x
    for ; n > 0; n /= 2 {
        if n&1 > 0 {
            ans = ans * x_contribute % mod
        }
        x_contribute = x_contribute * x_contribute % mod
    }
    return
}